
function getUser() {
    http.ajax({
        type: "get",
        url: "/admin/user/detail",
        success: function (response) {
            // 页面显示
            if (response.code === 200) {
                // 解构
                const { username, nickname, email, userPic, password } = response.data;
                $('#username').val(username);
                $('#nickname').val(nickname);
                $('#email').val(email);
                $('.user_pic').prop('src', userPic);
                $('#password').val(password);
            }
        }
    });
}


$('#exampleInputFile').on('change', () => {
    // 2.2 获取file对象
    const file = $('#exampleInputFile')[0].files[0];
    // 2.3 使用createObjectURL方法创建临时URL
    const url = URL.createObjectURL(file);
    // 2.4 把临时图片URL放回image标签的src属性
    $('.user_pic').attr('src', url);
})

function uploadImage() {
    const formData = new FormData($("#form")[0]);
    for (let item of formData.entries()) {
        console.log(item);
    };

    // const [username, nickname, email, userPic, password] = formData;
    // console.log(username, nickname, email, userPic, password);

    http.ajax({
        type: 'post',
        url: '/admin/user/edit',
        data: formData,
        success: (res) => {
            console.log(res);
            if (res.code === 200) {
                // parent.window.location.href = './index.html';
                const file = $("#exampleInputFile")[0].files[0];
                if (file) {
                    const tmpUrl = URL.createObjectURL(file);
                    console.log($('.sider .user_info img', parent.document));
                    $('.sider .user_info img', parent.document).prop('src', tmpUrl);
                    $('.header_bar .user_center_link img', parent.document).prop('src', tmpUrl);
                };
                $('.sider .user_info span', parent.document).html('欢迎&nbsp;$nbsp;' + $("#nickname").val());
            }
        }
    });

    return false;
}

getUser();

